﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private ATM atm = new ATM();
        private List<Account> accounts = new List<Account>(); // 存储多个账户
        private Account currentAccount;

        public Form1()
        {
            InitializeComponent();

            // 初始化一些账户
            accounts.Add(new Account("10001", 50000));          // 普通账户
            accounts.Add(new CreditAccount("20001", 1000, 5000)); // 信用账户
            accounts.Add(new Account("10002", 30000));         // 普通账户
            accounts.Add(new CreditAccount("20002", 500, 10000)); // 信用账户

            atm.BigMoneyFetched += OnBigMoneyFetched;
        }

        private void OnBigMoneyFetched(object sender, BigMoneyArgs e)
        {
            MessageBox.Show($"账户 {e.AccountId} 取款金额为 {e.Amount}，这是大笔金额交易！", "警告");
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentAccount == null)
                {
                    MessageBox.Show("请先登录。", "提示");
                    return;
                }

                decimal amount = decimal.Parse(textBox2.Text);
                atm.Deposit(currentAccount, amount);
                label3.Text = $"当前账户余额为: {currentAccount.Balance}";
                MessageBox.Show($"存款成功，当前账户余额为: {currentAccount.Balance}");
            }
            catch (BadCashException ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentAccount == null)
                {
                    MessageBox.Show("请先登录。", "提示");
                    return;
                }

                decimal amount = decimal.Parse(textBox2.Text);
                atm.Withdraw(currentAccount, amount);
                label3.Text = $"当前账户余额为: {currentAccount.Balance}";
                MessageBox.Show($"取款成功，当前账户余额为: {currentAccount.Balance}");
            }
            catch (BadCashException ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // 获取用户输入的账户ID
            string inputAccountId = textBox1.Text.Trim();

            // 根据输入的账户ID查找对应的账户
            currentAccount = accounts.FirstOrDefault(a => a.AccountId == inputAccountId);

            if (currentAccount != null)
            {
                label3.Text = $"当前账户余额为: {currentAccount.Balance}";
            }
            else
            {
                MessageBox.Show("找不到该账户，请检查输入的账户ID。", "错误");
            }

            // 检查账户类型
            if (currentAccount is CreditAccount creditAccount)
            {
                label4.Text = $"当前账户是信用账户，信用额度为: {creditAccount.CreditLimit}";
            }
            else
            {
                label4.Text = string.Empty; // 清空标签
            }
        }

    }


    // 自定义异常类
    public class BadCashException : Exception
    {
        public BadCashException(string message) : base(message) { }
    }

    // 事件参数类
    public class BigMoneyArgs : EventArgs
    {
        public string AccountId { get; }
        public decimal Amount { get; }

        public BigMoneyArgs(string accountId, decimal amount)
        {
            AccountId = accountId;
            Amount = amount;
        }
    }

    // 普通账号类
    public class Account
    {
        public string AccountId { get; private set; }
        public decimal Balance { get; protected set; }

        public Account(string accountId, decimal initialBalance)
        {
            AccountId = accountId;
            Balance = initialBalance;
        }

        public virtual void Deposit(decimal amount)
        {
            Balance += amount;
        }

        public virtual void Withdraw(decimal amount)
        {
            if (amount > Balance)
                throw new InvalidOperationException("余额不足");
            Balance -= amount;
        }
    }

    // 信用账号类，继承自Account
    public class CreditAccount : Account
    {
        public decimal CreditLimit { get; private set; }

        public CreditAccount(string accountId, decimal initialBalance, decimal creditLimit)
            : base(accountId, initialBalance)
        {
            CreditLimit = creditLimit;
        }

        public override void Withdraw(decimal amount)
        {
            if (amount > Balance + CreditLimit)
                throw new InvalidOperationException("超出信用额度");
            Balance -= amount;
        }
    }

    // ATM类
    public class ATM
    {
        public event EventHandler<BigMoneyArgs> BigMoneyFetched;

        public void Deposit(Account account, decimal amount)
        {
            Random random = new Random();
            if (random.NextDouble() < 0.3) // 30%的几率抛出异常
            {
                throw new BadCashException("检测到坏钞票！");
            }

            account.Deposit(amount);
        }

        public void Withdraw(Account account, decimal amount)
        {

            account.Withdraw(amount);

            if (amount > 10000)
            {
                BigMoneyFetched?.Invoke(this, new BigMoneyArgs(account.AccountId, amount));
            }
        }

    }

}